#include <iostream>
#include <stack>
using namespace std;

const int kMaxN = 2e5 + 10;
const int kMaxA = kMaxN;

int a[kMaxN];
int l[kMaxN], r[kMaxN];
stack<int> s;

long long GetExtremumSum(int n, int f) {
  long long ret = 0;

  a[0] = (kMaxA + 1) * f;
  s.push(0);
  for (int i = 1; i <= n; i++) {
    r[i] = n + 1;
    for (; a[s.top()] * f <= a[i] * f; s.pop()) {
      r[s.top()] = i;
    }
    l[i] = s.top();
    s.push(i);
  }
  for (int i = 1; i <= n; i++) {
    ret += (long long)a[i] * (i - l[i]) * (r[i] - i);
  }
  return ret;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  long long ans = 0;

  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  ans = GetExtremumSum(n, -1);
  cout << ans << "\n";
  return 0;
}